#include "StdAfx.h"
#include "StreamBuffer.h"

IStreamBuffer* StreamBuffer::CreateComponent()
{
	return new CStreamBuffer;
}

CStreamBuffer::CStreamBuffer(void)
	: m_readOffset(0)
	, m_length(0)	
{
}


CStreamBuffer::~CStreamBuffer(void)
{
}

void CStreamBuffer::Write( char* data, size_t len )
{
	memcpy(m_buffer+m_length, data, __min(sizeof(m_buffer)-m_length, len));
	Write(len);
}

void CStreamBuffer::Write( size_t len )
{
	m_length += __min(sizeof(m_buffer)-m_length, len);
}

void CStreamBuffer::Read( char* data, size_t len )
{
	memcpy(data, m_buffer+m_readOffset, __min(m_length-m_readOffset, len));
	Read(len);
}

void CStreamBuffer::Read( size_t len )
{
	m_readOffset += __min(m_length-m_readOffset, len);
}

char* CStreamBuffer::GetRaw()
{
	return m_buffer;
}

size_t CStreamBuffer::GetReadOffset()
{
	return m_readOffset;
}

size_t CStreamBuffer::GetLength()
{
	return m_length;
}

void CStreamBuffer::Release()
{
	delete this;
}
